<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Mod_khmerhr extends CI_Model{

	function getJobType(){
		return $this->db->get("tbl_jobtype");
	}

	function getLocation(){
		return $this->db->get("tbl_location");
	}
	function getAllEmployee(){
		$this->db->select("id,first_name,last_name");
		return $this->db->get("tbl_users");
	}

	function getDataTable($tbl_name,$orderName){
		$this->db->order_by($orderName, 'ASC');
		return $this->db->get($tbl_name);
	}
	function countTable($table,$name,$cateId){
		$this->db->where($name,$cateId);
		$this->db->from($table);
		return $this->db->count_all_results();
	}
	function getViewJobDetail($jobId){
		$this->db->where("tbl_job_post.job_post_id",$jobId);
		$this->db->join("tbl_job_time","tbl_job_time.job_time_id = tbl_job_post.job_time_id");
		$this->db->join("tbl_users","tbl_users.id = tbl_job_post.employeeId");
		$this->db->join("tbl_company","tbl_company.com_id = tbl_job_post.com_id");
		return $this->db->get("tbl_job_post");
	}
	function geJobLocation($id){
		$this->db->select("tbl_job_post.job_post_id,tbl_job_post.job_post_title,tbl_job_post.close_date,tbl_company.com_name,tbl_company.com_id,tbl_location.locationName");
		
		$this->db->from("tbl_post_province");
		$this->db->where("tbl_post_province.province_id",$id);
		$this->db->join("tbl_job_post","tbl_job_post.job_post_id = tbl_post_province.job_post_id");
	
		$this->db->join("tbl_location","tbl_post_province.province_id = tbl_location.locationID");
		
		$this->db->join("tbl_company","tbl_company.com_id = tbl_job_post.com_id");
		return $this->db->get();
	
	}
	function geJobCategory($cateId){
		$this->db->select("tbl_job_post.job_post_id,tbl_job_post.job_post_title,tbl_job_post.close_date,tbl_company.com_name,tbl_company.com_id");
		$this->db->from("tbl_post_cate");
		$this->db->where("tbl_post_cate.category_id",$cateId);
		$this->db->join("tbl_job_post","tbl_job_post.job_post_id = tbl_post_cate.job_post_id");
		$this->db->join("tbl_company","tbl_company.com_id = tbl_job_post.com_id");
	    return $this->db->get();
	}

	function register($firstName,$lastName,$email,$password){
		$dateCreate = strtotime(date("Y-m-d H:i:s"));
		$arr = array(
			"userName"=>$firstName.'.'.$lastName,
			"email"=>$email,
			"password"=>$password,
			"created_on"=>$dateCreate,
			"active"=>"1",
			"first_name"=>$firstName,
			"last_name"=>$lastName
		);
		$add = $this->db->insert("tbl_users",$arr);
		$id = $this->db->insert_id();
		$this->db->set("user_id",$id);
		$this->db->insert("tbl_users_groups");
		return $add;
	}
	function get_group($id){
		$this->db->select("*");
		$this->db->where("user_id",$id);
		$this->db->from("tbl_users_groups");
		$result = $this->db->get();
		return $result;
	}

	function checkMailExist($email){
		$this->db->where("email",$email);
		$this->db->limit(1);
		$result = $this->db->get("tbl_users");
		return $result->num_rows();
	}
	function getLogin($email,$password){
		$this->db->select("*");
		$this->db->from("tbl_users");
		$this->db->join("tbl_users_groups","tbl_users_groups.user_id = tbl_users.id");
		$this->db->where("tbl_users.email",$email);
		$this->db->where("tbl_users.password",$password);
		$this->db->where("tbl_users_groups.group_id !=",1);
		$this->db->limit(1);
		$query = $this->db->get();
        if($query->num_rows() == 1) { 

            return $query->row(); 
        } else {
            return false; 
        }
	}
	function getLocationName($jobId){
		$this->db->where("tbl_post_province.job_post_id",$jobId);
		$this->db->join("tbl_location","tbl_location.locationID = tbl_post_province.province_id");
		return $this->db->get("tbl_post_province");
	}

}